/* game.c contains the game engine */

#include "game.hxx"
#include "execbasic.hxx"

void gameEngine(void)
{
	char quit = FALSE;
	char key0 = 0, key1 = 0,key2 = 0, key3 = 0;
	char lifeText[4];
	/* directions is 4 bit
		0000
		^^^^
		|||--- were going right
		||--- were going down
		|--- were going up
		--- were going left
	*/
	unsigned int directions = 0;
	unsigned int direction1 = 0;
	unsigned int direction2 = 0;
	sprintf(lifeText, "%d", health);
	SDL_Surface *player;
	SDL_Surface *temp;
	
//	SDL_Rect playerRect;
	
	//SFont_Font *selFont;

	if ((temp = IMG_Load("data/graphics/ship-s.png")) == NULL) logger.log(2, "image error (player)");
	player = SDL_DisplayFormat(temp);
	SDL_FreeSurface(temp);
	if ((temp = IMG_Load("data/graphics/fire-s.png")) == NULL) logger.log(2, "image error (fire)");
	fire = SDL_DisplayFormat(temp);
	SDL_FreeSurface(temp);	
	if ((temp = IMG_Load("data/graphics/badguy-s.png")) == NULL) logger.log(2, "image error (badguy)");
	badguy = SDL_DisplayFormat(temp);
	SDL_FreeSurface(temp);
	//if ((temp = IMG_Load("data/graphics/neonbluefont.png")) == NULL) logger.log(2, "image error (font)");
	//slFont = SDL_DisplayFormat(temp);
	//SDL_FreeSurface(temp);
	//selFont = SFont_InitFont(slFont);
	SDL_SetColorKey(player, SDL_SRCCOLORKEY | SDL_RLEACCEL, (Uint16)SDL_MapRGB(player->format,223,0,223));
	SDL_SetColorKey(fire, SDL_SRCCOLORKEY | SDL_RLEACCEL, (Uint16)SDL_MapRGB(fire->format, 223,0,223));
	SDL_SetColorKey(badguy, SDL_SRCCOLORKEY | SDL_RLEACCEL, (Uint16)SDL_MapRGB(badguy->format, 223,0,223));

	playerRect.x = 370;
	playerRect.y = 500;
	playerRect.w = 60;
	playerRect.h = 80;

	logger.log(0, "you've chosen '%s' as your destiny :D", scro.levelfile);
	
	/* DONE: Check if level valid! 		*/
//	logger.log(0, "checking if a valid level file...");
	if (sli_check_file(scro.levelfile) != 0) quitScro(SLI_ERROR_INVALIDLEVEL);
	
	Scroll_LevelType * scro_level = sli_return_level(scro.levelfile);
	Scroll * scroll_engine = new Scroll(25, 19, scro_level);
	
	scroll_engine->Blit(screen);

	SDL_Flip(screen);
	
	logger.log(0, "reaching main loop...");
	while (quit == FALSE)
	{
		scroll_engine->Move(0,-1);
		scroll_engine->Blit(screen);
		SDL_BlitSurface(player, NULL, screen, &playerRect);
		doObjectStuff();
		SDL_Flip(screen);
	
		if (SDL_PollEvent(&event))
		{
			if (event.type == SDL_KEYDOWN)
			{
				if (event.key.keysym.sym == SDLK_RETURN) //OKE_KEY
				{
					quit = TRUE;
				}

				if (event.key.keysym.sym == SDLK_LEFT)
				{
					directions |= 0x1000;
				}
				if (event.key.keysym.sym == SDLK_RIGHT)
				{
					directions |= 0x0001;
				}
				if (event.key.keysym.sym == SDLK_UP)
				{
					directions |= 0x0100;
				}
				if (event.key.keysym.sym == SDLK_DOWN)
				{
					directions |= 0x0010;
				}
				if (event.key.keysym.sym == SDLK_SPACE)
				{
					generateNewObject(1);
				}
				if (event.key.keysym.sym == SDLK_TAB)
				{
					generateNewObject(2);
				}
			}
			if (event.type == SDL_KEYUP)
			{
				if (event.key.keysym.sym == SDLK_LEFT) directions ^= 0x1000;
				if (event.key.keysym.sym == SDLK_RIGHT) directions ^= 0x0001;
				if (event.key.keysym.sym == SDLK_UP) directions ^= 0x0100;
				if (event.key.keysym.sym == SDLK_DOWN) directions ^= 0x0010;
			}
		}
		if (directions & 0x1000) playerRect.x -= PLAYER_SPEED;
		if (directions & 0x0001) playerRect.x += PLAYER_SPEED;
		if (directions & 0x0010) playerRect.y += PLAYER_SPEED;
		if (directions & 0x0100) playerRect.y -= PLAYER_SPEED;
		if (playerRect.x > 720) playerRect.x = 720;
		if (playerRect.x < 21) playerRect.x = 21;
		if (playerRect.y > 500) playerRect.y = 500;
		if (playerRect.y < 20) playerRect.y = 20;
		SDL_Delay(TimeLeft());
	}
/*	
	while ((levelScreenRect0.y > 0) && (quit == FALSE))
	{
		if (SDL_PollEvent(&event))
		{
			if (event.type == SDL_KEYDOWN)
			{
				if (event.key.keysym.sym == SDLK_RIGHT)
				{
					key0 = 1;
				}
				if (event.key.keysym.sym == SDLK_LEFT) 
				{
					key1 = 1;
				} 
				if (event.key.keysym.sym == SDLK_UP)
				{
					key2 = 1;
				}
				if (event.key.keysym.sym == SDLK_DOWN)
				{ 
					key3 = 1;
				}
				if (event.key.keysym.sym == OKE_KEY) 
				{
					quit = TRUE;
				}
				if (event.key.keysym.sym == SDLK_SPACE)
				{
					generateNewObject(1);
				}
				if (event.key.keysym.sym == SDLK_TAB)
				{
					generateNewObject(2);
				}
			}
			if (event.type == SDL_KEYUP)
			{
				if (event.key.keysym.sym == SDLK_RIGHT) key0 = 0;;
				if (event.key.keysym.sym == SDLK_LEFT) key1 = 0;
				if (event.key.keysym.sym == SDLK_UP) key2 = 0;
				if (event.key.keysym.sym == SDLK_DOWN) key3 = 0;
			}
		}
		if (key0 == 1) playerRect.x += PLAYER_SPEED;
		if (key1 == 1) playerRect.x -= PLAYER_SPEED;
		if (key2 == 1) playerRect.y -= PLAYER_SPEED;
		if (key3 == 1) playerRect.y += PLAYER_SPEED;
		if (playerRect.x > 720) playerRect.x = 720;
		if (playerRect.x < 21) playerRect.x = 21;
		if (playerRect.y > 500) playerRect.y = 500;
		if (playerRect.y < 20) playerRect.y = 20;
		sprintf(lifeText, "%d", health);
		
		SDL_BlitSurface(player, NULL, screen, &playerRect);
		doObjectStuff();
		SFont_Write(screen, selFont, 20, 15, lifeText);
		levelScreenRect0.y -= 10;
		
	}
	waitForKey(OKE_KEY);*/
	delete scroll_engine;
	sli_free_level(scro_level);
	SDL_FreeSurface(player);
//	SFont_FreeFont(selFont);
}


void generateNewObject(unsigned int id)
{
	unsigned char welches = 0;
	if (id != 0)
	{
		while ((objectArray[welches].id != 0) && (welches <= MAX_OBJECTS))
		{
			welches++;
		}
		if (objectArray[welches].id == 0)
		{
			if (id == 1) // fireball
			{
				objectArray[welches].id = 1;
				objectArray[welches].status = 0;
				//objectArray[welches].x = playerRect.x + playerRect.w/2 - 10;
				//objectArray[welches].y = playerRect.y;
				objectArray[welches].rect.x = playerRect.x + playerRect.w/2 - 10;
				objectArray[welches].rect.y = playerRect.y;
				objectArray[welches].rect.w = 20;
				objectArray[welches].rect.h = 20;
			}
			if (id == 2) // badguy
			{
				objectArray[welches].id = 2;
				objectArray[welches].status = 0;
				//objectArray[welches].x = 370;
				//objectArray[welches].y = -60;
				objectArray[welches].rect.x = 370;
				objectArray[welches].rect.y = 0;
				objectArray[welches].rect.w = 60;
				objectArray[welches].rect.h = 60;
			}
		}
	}
}

void doObjectStuff(void)
{
	unsigned char welches = 0;
	unsigned char welches2 = 0;
	//SDL_Rect objectRect;
	while (welches < MAX_OBJECTS)
	{
		if (objectArray[welches].id == 1) //fireball
		{
			objectArray[welches].rect.y -= FIRE_SPEED;
			//objectRect.x = objectArray[welches].x;
			//objectRect.y = objectArray[welches].y;
			if (objectArray[welches].rect.y < 21) 
			{ 
				objectArray[welches].id = 0;
			} else
			{
				SDL_BlitSurface(fire, NULL, screen, &objectArray[welches].rect);
			}
		}
		if (objectArray[welches].id == 2) //badguy
		{
			objectArray[welches].rect.y += PLAYER_SPEED;
			//objectRect.x = objectArray[welches].x;
			//objectRect.y = objectArray[welches].y;
			if (objectArray[welches].rect.y > 860)
			{
				objectArray[welches].id = 0;
			} else
			{
				SDL_BlitSurface(badguy, NULL, screen, &objectArray[welches].rect);
			}
		}
		welches++;
	}
	
	// simple collision-detection 
	welches = 0;
	//struct object target;
	while (welches < MAX_OBJECTS)
	{
		if (objectArray[welches].id > 0)
		{
			welches2 = 0;
			while (welches2 < MAX_OBJECTS)
			{
				//logger.log(0, "welches %d welches2 %d", welches, welches2);
				if (objectArray[welches2].id > 0)
				{
					if (objectArray[welches].id == 1) // fireball
					{
						if (objectArray[welches2].id == 2)
						{
							//logger.log(0, "fireball + badguy detected");
							//if (rectOverlap(objectArray[welches].rect, objectArray[welches2].rect))
							if (SDL_CollideBoundingBox2(objectArray[welches].rect,objectArray[welches2].rect))
							{
								//logger.log(0, "collision detected!");
								objectArray[welches].id = 0;
								objectArray[welches2].id = 0;								
							}
						}
					}
					if (objectArray[welches].id == 2) //badguy
					{
						if (SDL_CollideBoundingBox2(objectArray[welches].rect, playerRect))
						{
							objectArray[welches].id = 0;
							health -= 1;
						}
					}
				}
				welches2++;
			}
		}
		welches++;
	}
	
}

